package leetcode100;

// TODO 脑筋急转弯 | 快慢指针 【-】 删除链表的倒数第 N 个结点
// TODO https://leetcode.cn/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-b-61/

public class lc100_0019 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast = head;
        ListNode slow = new ListNode(), tmp = slow;
        slow.next = head;
        // 快指针先走 n 步
        for (int i=0; i<n; i++) {
            fast = fast.next;
        }
        // 快慢指针一起走
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return tmp.next;
    }

}